package cn.hpclub.server.controller.admin;

import cn.hpclub.server.model.Admin;
import cn.hpclub.server.service.DzgDailyService;
import cn.hpclub.server.util.DateKit;
import cn.hpclub.server.util.ExcelRenderUtil;
import cn.hpclub.server.util.excel.ExportUtils;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by wanglin on 2017/3/8.
 */
public class DzgDailyController extends BaseAdminController<Admin> {
    private static final Logger logger            = LoggerFactory.getLogger(DzgDailyController.class);

    public void listMonthly(){
        String theDate = getPara("the_date","");

        if(theDate.isEmpty()){
            theDate= DateKit.toStr(new Date(),"yyyy-MM");
        }
        setAttr("the_date",theDate);
        List<Record> list =  DzgDailyService.service.listMonthly(theDate);
        setAttr("list",list);
        render("/admin/monthly_report_list.tpl");
    }

    public void monthlyExport(){
        String theDate = getPara("the_date", "");

        if(theDate.isEmpty()){
            theDate= DateKit.toStr(new Date(),"yyyy-MM");
        }

        String[] excelHeaders = new String[]{"地市","当月累计活跃人数","宽带预约当月累计办理量","开放渠道当月累计办理量","宽带开户当月累计办理量","业务当月累计办理量"};
        ExportUtils exportUtils = new ExportUtils(excelHeaders);
        String[] keys = new String[]{"city","activeNumber","srNumber","classNumber","booNumber","total"};
        List<Record> list =  DzgDailyService.service.listMonthly(theDate);
        try {
            exportUtils.exportExcel(keys,list,null,1);//此处最后一个参数表示从第一行开始填入数据（表头为第0行）
        } catch (IOException e) {
            logger.info("导出月报列表错误！");
        }
        SXSSFWorkbook workbook = exportUtils.getSXSSFWorkbook();
        render(new ExcelRenderUtil(workbook, "大掌柜月报"));
    }

    public void dailyList(){
        String theDate = getPara("the_date", "");

        if(theDate.isEmpty()){
            theDate= DateKit.getLastDay();
        }
        setAttr("the_date",theDate);
        List<Record> list =  DzgDailyService.service.getDailyList(theDate);
        setAttr("list",list);
        render("/admin/daily_report_list.tpl");
    }

    public void dailyExport(){
        String theDate = getPara("the_date", "");

        if(theDate.isEmpty()){
            theDate= DateKit.getLastDay();
        }

        String[] excelHeaders = new String[]{"地市","注册渠道数","当日活跃渠道数","注册人员数","当日活跃人员数","当日4G资费办理量","当日4G流量办理量","当日营销活动办理量","当日宽带预约办理量","当日宽带开户办理量","当日开户办理量","当日实名补登记办理量","当日精彩业务办理量","当日充值笔数","当日充值金额"};
        ExportUtils exportUtils = new ExportUtils(excelHeaders);
        String[] keys = new String[]{"city","register_channel_count","active_channel_count","register_member_count","active_member_count","postage_count","traffic_count","marketing_activities_count","service_reservation_count","broadband_count","open_count","register_realname_count","good_business_count","recharge_count","rechargeAmount"};
        List<Record> list =  DzgDailyService.service.getDailyList(theDate);
        try {
            exportUtils.exportExcel(keys,list,null,1);//此处最后一个参数表示从第一行开始填入数据（表头为第0行）
        } catch (IOException e) {
            logger.info("导出日报列表错误！");
        }
        SXSSFWorkbook workbook = exportUtils.getSXSSFWorkbook();
        render(new ExcelRenderUtil(workbook, "大掌柜日报"));
    }
}
